﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;


namespace MyWebPagesStarterKit
{
    /// <summary>
    /// Summary description for DownloadManager
    /// </summary>
    public class DownloadManager : Section<DownloadManager.Data>
    {
        public DownloadManager(){}
        public DownloadManager(string id) : base(id) { }


        /// <summary>
        /// List of all folder
        /// </summary>
        public List<VersionFile> PublicFolders
        {
            get
            {
                List<VersionFile> locatedObjects = null;

                if (_data.VersionFilesList != null)
                    locatedObjects = _data.VersionFilesList.FindAll(delegate(VersionFile s) { return s.IsFolder == true && s.Active == true; });

                return locatedObjects;
            }
        }
           /// <summary>
        /// List of all folder
        /// </summary>
        public List<VersionFile> AdminFolders
        {
            get
            {
                List<VersionFile> locatedObjects = null;

                if (_data.VersionFilesList != null)
                    locatedObjects = _data.VersionFilesList.FindAll(delegate(VersionFile s) { return s.IsFolder == true; });

                return locatedObjects;
            }
        }

        /// <summary>
        /// List of all files contained in the folder
        /// </summary>
        public List<VersionFile> AdminFilesInFolder(string parentID)
        {
            List<VersionFile> locatedObjects = null;

            if (_data.VersionFilesList != null)
                locatedObjects = _data.VersionFilesList.FindAll(delegate(VersionFile s) { return s.ParentOfFolder == parentID && (s.IsFolder == false); });

            return locatedObjects;  
        }

        /// <summary>
        /// List of all files contained in the folder
        /// </summary>
        public List<VersionFile> PublicFilesInFolder(string parentID)
        {
            List<VersionFile> locatedObjects = null;

            if (_data.VersionFilesList != null)
                locatedObjects = _data.VersionFilesList.FindAll(delegate(VersionFile s) { return s.ParentOfFolder == parentID && (s.IsFolder == false) && s.Active == true; });
      
            return locatedObjects;  
        }


        /// <summary>
        /// Get all files
        /// </summary>
        public VersionFile GetFolder(string itemID)
        {
            VersionFile locatedObject = _data.VersionFilesList.Find(delegate(VersionFile s) { return s.FileID==itemID; });
            return locatedObject;
        }

        /// <summary>
        /// Get all files
        /// </summary>
        public VersionFile FilesInFolder(string itemID)
        {
            VersionFile locatedObject = _data.VersionFilesList.Find(delegate(VersionFile s) { return s.FileID == itemID; });
            return locatedObject;
        }


        /// <summary>
        /// Add anew folder or item
        /// </summary>
        /// <param name="title"></param>
        /// <param name="comment"></param>
        /// <param name="filename"></param>
        /// <param name="version"></param>
        /// <param name="active"></param>
        /// <param name="isfolder"></param>
        /// <param name="parentof"></param>
        public void Add(string id,string title, string comment,string filename, string version, bool active, bool isfolder, string parentof)
        {
            VersionFile file = new VersionFile(id,title,comment,filename, version, DateTime.Now, DateTime.Now, active, isfolder, parentof, 0);
            _data.VersionFilesList.Add(file);
            SaveData();
        }

        public void Remove(string id)
        {
            VersionFile locatedObject = _data.VersionFilesList.Find(delegate(VersionFile s) { return s.FileID == id; });
            _data.VersionFilesList.Remove(locatedObject);
            SaveData();
        }


        public override List<SearchResult> Search(string searchString, WebPage page)
        {
            List<SearchResult> foundResults = new List<SearchResult>();
            return foundResults;
        }

        public override bool IsOnline
        {
            get{   return _data.IsOnline;}
            set{  _data.IsOnline = value;}
        }

        public override Position Position
        {
            get { return _data.Position; }
            set{ _data.Position = value; }
        }

        public class Data
        {
            public List<VersionFile> VersionFilesList = new List<VersionFile>();
            public bool IsOnline = false;
            public Position Position;
        }



        [Serializable]
        public class VersionFile
        {
            private string _FileID;
            private string _Title;
            private string _Comment;
            private string _FileName;
            private string _Version;
            private DateTime _DateAdded;
            private DateTime _DateChanged;
            private bool _Active;
            private bool _IsFolder;
            private string _ParentOfFolder;
            private int _DownloadCount;

            public VersionFile() { }

            public VersionFile(string id)
            {
                this._FileID = id;
            }

            public VersionFile(string id,string title, string comment,string filename, string version,DateTime added, DateTime changed,bool active,bool isfolder,string parentof,int downloadcount)
            {
                this._FileID = id;
                this._Title = title;
                this._Comment = comment;
                this._FileName=filename;
                this._Version = version;
                this._DateAdded = added;
                this._DateChanged = changed;
                this._Active=active;
                this._IsFolder= isfolder;
                this._ParentOfFolder=parentof;
                this._DownloadCount = downloadcount;
            }

            public string FileID
            {
                get{return this._FileID;}
                set{this._FileID = value;}
            }

            public string Title
            {
                get{return this._Title;}
                set{this._Title = value;}
            }

            public string Comment
            {
                get{return this._Comment;}
                set{this._Comment = value;}
            }

            public string FileName
            {
                get{return this._FileName;}
                set{this._FileName = value;}
            }

            public string Version
            {
                get{return this._Version;}
                set{this._Version = value;}
            }

            public DateTime DateAdded
            {
                get{return this._DateAdded;}
                set{this._DateAdded = value;}
            }

            public DateTime DateChanged
            {
                get{return this._DateChanged;}
                set{this._DateChanged = value;}
            }

             public string ParentOfFolder
            {
                get{return this._ParentOfFolder;}
                set{this._ParentOfFolder = value;}
            }

            public bool Active
            {
                get{return this._Active;}
                set{this._Active = value;}
            }

            public bool IsFolder
            {
                get{return this._IsFolder;}
                set{this._IsFolder = value;}
            }
            public int DownloadCount
            {
                get{return this._DownloadCount;}
                set{this._DownloadCount = value;}
            }
        }

       
    }
}
